//
//  KATWaitWindow.m
//  KATFramework
//
//  Created by Kantice on 15/9/18.
//  Copyright (c) 2015年 KatApp. All rights reserved.
//

#import "KATWaitWindow.h"



@interface KATWaitWindow ()

///内容
@property(nonatomic,retain) KATLabel *infoLabel;

///状态
@property(nonatomic,assign) int state;

///隐藏定时器
@property(nonatomic,retain) NSTimer *hideTimer;


@end



@implementation KATWaitWindow


//获取实例(切片动画)
+ (instancetype)windowWithPieceAnimation:(KATPieceAnimationView *)pieceAnimation
{
    return [self windowWithStyle:KATWaitWindowStyleAlpha animationView:pieceAnimation info:nil infoStyle:nil];
}


//获取实例(等待样式)
+ (instancetype)waitingWithStyle:(KATWaitWindowStyle)style
{
    KATWaitingAnimationView *anim=[KATWaitingAnimationView animationWithFrame:CGRectMake(0, 0, sqrt(MIN(SCREEN_MIN,500.0))*3.2, sqrt(MIN(SCREEN_MIN,500.0))*3.2)];
    
    //样式
    UIColor *color=nil;
    
    if(style==KATWaitWindowStyleDark)//深色样式
    {
        color=KCOLOR_WHITE;
    }
    else if(style==KATWaitWindowStyleAlpha)//透明样式
    {
        color=KCOLOR_WHITE;
    }
    else//亮色样式
    {
        color=KCOLOR_DARK;
    }
    
    anim.ringShape.strokeColor=color.CGColor;
    
    KATWaitWindow *window=[self windowWithStyle:style animationView:anim info:nil infoStyle:nil];
    
    window.contentWidth=window.animationView.bounds.size.width+window.contentPadding*2;
    
    return window;
}


//获取实例(加载样式)
+ (instancetype)loadingWithStyle:(KATWaitWindowStyle)style
{
    KATWaitWindow *window=[self loadingWithInfo:nil andStyle:style];
    window.contentWidth=window.animationView.bounds.size.width+window.contentPadding*2;
    
    return window;
}


//获取实例(带文本的加载样式)
+ (instancetype)loadingWithInfo:(NSString *)info andStyle:(KATWaitWindowStyle)style
{
    KATLoadingAnimationView *anim=[KATLoadingAnimationView animationWithFrame:CGRectMake(0, 0, sqrt(MIN(SCREEN_MIN,500.0))*3.2, sqrt(MIN(SCREEN_MIN,500.0))*3.2)];
    
    //样式
    UIColor *color=nil;
    
    if(style==KATWaitWindowStyleDark)//深色样式
    {
        color=KCOLOR_WHITE;
    }
    else if(style==KATWaitWindowStyleAlpha)//透明样式
    {
        color=KCOLOR_WHITE;
    }
    else//亮色样式
    {
        color=KCOLOR_DARK;
    }

    anim.ringShape.strokeColor=color.CGColor;
    anim.successShape.strokeColor=color.CGColor;
    anim.failShape.strokeColor=color.CGColor;
    
    return [self windowWithStyle:style animationView:anim info:info infoStyle:nil];
}


//获取实例(全参数)
+ (instancetype)windowWithStyle:(KATWaitWindowStyle)style animationView:(UIView<KATAnimationViewDelegate> *)animationView info:(NSString *)info infoStyle:(KATTextStyle *)iStyle
{
    KATWaitWindow *window=[[[self alloc] init] autorelease];
    
    [window initData];
    
    window.isTapShadowToHide=YES;
    window.isTapContentToHide=YES;
    window.isBlurStyle=YES;
    window.showMode=KATPopWindowAnimationTypeAlpha;
    window.hideMode=KATPopWindowAnimationTypeAlpha;
    
    [window initWindow];
    
    
    //样式
    window.contentWidth=sqrt((MIN(SCREEN_WIDTH, SCREEN_HEIGHT)))*8.0;
    window.contentPadding=sqrt((MIN(SCREEN_WIDTH, SCREEN_HEIGHT)))*0.8;
    
    if(!iStyle)
    {
        iStyle=[KATTextStyle textStyle];
        
        iStyle.textFont=[UIFont systemFontOfSize:window.contentPadding*1.1];
        iStyle.textColor=KCOLOR_DARK;
        iStyle.hasDateStyle=NO;
        iStyle.textAlignment=NSTextAlignmentCenter;
        iStyle.marginTop=1;
        iStyle.marginLeft=1;
        iStyle.marginRight=1;
        iStyle.marginBottom=1;
    }
    
    window.infoLabel=[KATLabel labelWithFrame:CGRectZero andStyle:nil];
    
    [window.content addSubview:window.infoLabel];
    
    
    window.style=style;
    window.animationView=animationView;
    window.info=info;
    window.infoStyle=iStyle;
    window.hideWaitDuration=2.0;
    window.isAutoHide=YES;
    window.eventDelegate=nil;
    
    [window updateWindow];
    
    return window;
}



- (void)setInfo:(NSString *)info
{
    if(info!=_info)
    {
        [_info release];
        _info=[info copy];
        
        [self updateWindow];
    }
}


- (void)setInfoStyle:(KATTextStyle *)infoStyle
{
    if(infoStyle!=_infoStyle)
    {
        [_infoStyle release];
        _infoStyle=[infoStyle retain];
        
        [self updateWindow];
    }
}


- (void)setAnimationView:(UIView<KATAnimationViewDelegate> *)animationView
{
    if(_animationView!=animationView)
    {
        [_animationView removeFromSuperview];
        
        [_animationView release];
        _animationView=[animationView retain];
        
        [self.content addSubview:_animationView];
        
        [self updateWindow];
    }
}


- (void)setStyle:(KATWaitWindowStyle)style
{
    if(_style!=style)
    {
        _style=style;
        
        [self updateWindow];
    }
}


- (void)setContentWidth:(float)contentWidth
{
    _contentWidth=contentWidth;
    
    [self updateWindow];
}


- (void)setContentPadding:(float)contentPadding
{
    _contentPadding=contentPadding;
    
    [self updateWindow];
}




//更新窗口
- (void)updateWindow
{
    [self performSelectorOnMainThread:@selector(_updateWindow) withObject:nil waitUntilDone:YES];
}


//更新窗口(内部方法)
- (void)_updateWindow
{
    //样式
    if(_style==KATWaitWindowStyleDark)//深色样式
    {
        self.bgColor=KCOLOR_SHADOW_DARK;
        self.isBlurStyle=NO;
        self.shadowColor=KCOLOR_SHADOW;
        _infoStyle.textColor=KCOLOR_WHITE;
    }
    else if(_style==KATWaitWindowStyleAlpha)//透明样式
    {
        self.bgColor=KCOLOR_CLEAR;
        self.shadowColor=KCOLOR_SHADOW_LIGHT;
        self.isBlurStyle=NO;
    }
    else//亮色样式
    {
        self.isBlurStyle=YES;
        self.content.backgroundColor=[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.4];
        self.blurView.effect=[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
        self.shadowColor=KCOLOR_SHADOW;
        _infoStyle.textColor=KCOLOR_DARK;
    }
    
    if(_animationView)
    {
        _animationView.frame=CGRectMake((_contentWidth-_animationView.bounds.size.width)*0.5, _contentPadding, _animationView.bounds.size.width, _animationView.bounds.size.height);
        
        if(!_info)//只有动画视图
        {
            _animationView.frame=CGRectMake(_contentPadding, _contentPadding, _animationView.bounds.size.width, _animationView.bounds.size.height);
        }
        
        _infoLabel.frame=CGRectMake(_contentPadding, _animationView.frame.origin.y+_animationView.frame.size.height+_contentPadding, _contentWidth-2*_contentPadding, 0);
    }
    else
    {
        _infoLabel.frame=CGRectMake(_contentPadding, _contentPadding, _contentWidth-2*_contentPadding, 0);
    }
    
    _infoLabel.text=_info;
    
    if(_info)
    {
        _infoLabel.style=_infoStyle;
        [_infoLabel adjustFrame];
    }
    else
    {
        _infoLabel.frame=CGRectMake(_contentPadding, _animationView.frame.origin.y+_animationView.frame.size.height, _contentWidth-2*_contentPadding, 0);
    }
    
    self.contentSize=CGSizeMake(_contentWidth, _infoLabel.frame.origin.y+_infoLabel.frame.size.height+_contentPadding);
    
    if(!_info && _animationView)//只有动画视图
    {
        self.contentSize=CGSizeMake(_animationView.bounds.size.width+_contentPadding*2, _infoLabel.frame.origin.y+_infoLabel.frame.size.height+_contentPadding);
    }
}



//显示
- (void)show
{
    [self performSelectorOnMainThread:@selector(_show) withObject:nil waitUntilDone:YES];
}


//显示(内部方法)
- (void)_show
{
    [self updateWindow];
            
    //禁止点击
    self.isTapShadowToHide=NO;
    self.isTapContentToHide=NO;
    
    if(_animationView)//开始动画
    {
        if([_animationView respondsToSelector:@selector(replay)])//优先考虑replay
        {
            [_animationView replay];
        }
        else if([_animationView respondsToSelector:@selector(play)])//没有replay的情况下使用play
        {
            [_animationView play];
        }
    }
    
    [super show];
}


//隐藏
- (void)hide
{
    [self performSelectorOnMainThread:@selector(_hide) withObject:nil waitUntilDone:YES];
}


//隐藏(内部方法)
- (void)_hide
{
    [super hide];
    
    if(_hideTimer)
    {
        [_hideTimer invalidate];
        [_hideTimer release];
        _hideTimer=nil;
    }
    
    if(_animationView)//停止动画
    {
        if([_animationView respondsToSelector:@selector(stop)])
        {
            [_animationView stop];
        }
    }
}


//完成
- (void)finish
{
    [self finishWithValue:0 andMessage:nil];
}


//完成
- (void)finishWithValue:(int)value
{
    [self finishWithValue:value andMessage:nil];
}


//完成
- (void)finishWithMessage:(NSString *)message
{
    [self finishWithValue:0 andMessage:message];
}


//完成
- (void)finishWithValue:(int)value andMessage:(NSString *)message
{
    self.info=message;
    
    [self performSelectorOnMainThread:@selector(_finishWithValue:) withObject:@(value) waitUntilDone:YES];
}


//完成(内部方法)
- (void)_finishWithValue:(id)value
{
    //恢复点击
    self.isTapShadowToHide=YES;
    self.isTapContentToHide=YES;
    
    if(_animationView)//完成动画
    {
        if([_animationView respondsToSelector:@selector(finishWithValue:)])//优先考虑finish
        {
            [_animationView finishWithValue:[value intValue]];
        }
        else if([_animationView respondsToSelector:@selector(stop)])//没有finish则使用stop
        {
            [_animationView stop];
        }
    }
    
    //自动隐藏
    if(_isAutoHide)
    {
        if(_hideTimer)
        {
            [_hideTimer invalidate];
            [_hideTimer release];
            _hideTimer=nil;
        }
        
        self.hideTimer=[NSTimer scheduledTimerWithTimeInterval:_hideWaitDuration target:self selector:@selector(hide) userInfo:nil repeats:NO];
    }
    
    //回调
    if(_eventDelegate && [_eventDelegate respondsToSelector:@selector(waitWindow:didFinishWithValue:)])
    {
        [_eventDelegate waitWindow:self didFinishWithValue:[value intValue]];
    }
}


//调整窗口(方向改变系统通知处理)
- (void)resizeWindow
{
    [super resizeWindow];
}


- (void)dealloc
{
    if(_hideTimer)
    {
        [_hideTimer invalidate];
        [_hideTimer release];
        _hideTimer=nil;
    }
    
    [_infoLabel release];
    [_info release];
    [_infoStyle release];
    [_animationView release];
    
    [super dealloc];
}


@end
